﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using io = System.IO;
using System.Globalization;
using MOJsdk;
using System.Windows;

namespace MOJ
{
	public class ConvertThumbPathToImage : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			MojWebImage name = value as MojWebImage;
			BitmapImage img;

			name.DownloadFiles();
			img = new BitmapImage(new Uri(name.ThumbPath));

			return img;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	public class ConvertImagePathToBitmapImage : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			string name = value as string;
			BitmapImage img;

			img = new BitmapImage(new Uri(name));

			return img;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	#region ListViewMojFiles
	public class ConvertFileTypeTopImageOfFileType : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			switch((int)value)
			{
				case 2:
					return new BitmapImage(new Uri(GlobalVars.TheHost.SystemVars.GetVar("MoJPosterIcon_20x20")));

				case 0:
					return new BitmapImage(new Uri(GlobalVars.TheHost.SystemVars.GetVar("MoJMovieIcon_20x20")));

				case 1:
					return new BitmapImage(new Uri(GlobalVars.TheHost.SystemVars.GetVar("MoJSubtitleIcon_20x20")));
			}

			return null;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

    public class ConvertFileIndex : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            
            int index = (int)value;
            MOJBasicFileClass file = GlobalVars.TheHost.GetFile(index);
            index = file.FileIndex;

            if (index < 1)
                return "";

            Button btn = new Button();
            btn.Style = GlobalVars.TheHost.LoadStyle("DefaultButton");
            btn.Content = index;
            btn.Click += delegate { SelectIndex(file.FileID); };
            btn.MinWidth = 30;

            return btn;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private void SelectIndex(int FileID)
        {
            MOJBasicFileClass file = GlobalVars.TheHost.GetFile(FileID);
            MOJMovie m = GlobalVars.TheHost.LoadMovie(file.MovieCode);
            Control_SelectIndex _ctrl = new Control_SelectIndex(m.MovieFiles.Count);
            WindowControl _win;

            GlobalVars.TheHost.SetForegroundGrid(Visibility.Visible);

            _win = GlobalVars.TheHost.AddNewUserControl(_ctrl, new WindowControlInfo(120, 100)
            {
                DefaultPosition = WindowPosition.Center,
                ZIndex = 1000,
                WindowTitle = MOJLocalization.Localize("Index"),
            });

            _ctrl.OnIndexSelected += delegate(int index)
            {
                _win.Close();
                file.FileIndex = index;
                file.UpdateInfo();
                ((Control_ImportMovies)GlobalVars.TheHost.GetElement("MOJ_managemovies")).UpdateList();
            };

            _win.OnClosed += delegate
            {
                GlobalVars.TheHost.SetForegroundGrid(Visibility.Hidden);
            };
        }
    }

    public class ConvertLanguageCode : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int langID = (int)value;
            MOJBasicFileClass file = GlobalVars.TheHost.GetFile(langID);
            langID = file.ISO639_Code;
            string lang;

            if (langID == 0)
                return "";
            else
                lang = ISO639_1.LoadLanguage(langID).Country;

            Button btn = new Button();
            btn.Style = GlobalVars.TheHost.LoadStyle("DefaultButton");
            btn.Content = lang;
            btn.Click += delegate { SelectLanguage(file.FileID); };
            btn.MinWidth = 30;

            return btn;
        }

        private void SelectLanguage(int FileID)
        {
            MOJBasicFileClass file = GlobalVars.TheHost.GetFile(FileID);
            Control_SelectLanguage _ctrl = new Control_SelectLanguage();
            WindowControl _win;

            GlobalVars.TheHost.SetForegroundGrid(Visibility.Visible);

            _win = GlobalVars.TheHost.AddNewUserControl(_ctrl, new WindowControlInfo(200, 450)
            {
                DefaultPosition = WindowPosition.Center,
                ZIndex = 1000,
                WindowTitle = MOJLocalization.Localize("Languages"),
            });

            _ctrl.OnSelectLanguage += delegate(ISO639_1 Lang)
            {
                _win.Close();
                file.ISO639_Code = Lang.Index;
                file.UpdateInfo();
                ((Control_ImportMovies)GlobalVars.TheHost.GetElement("MOJ_managemovies")).UpdateList();
            };

            _win.OnClosed += delegate
            {
                GlobalVars.TheHost.SetForegroundGrid(Visibility.Hidden);
            };
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
	#endregion
}
